package org.game.db.dao;

import com.sojoys.artifact.manager.DataSourceManager;
import java.sql.Connection;
import com.sojoys.artifact.build.data.base.BaseDao;
import java.util.ArrayList;
import com.sojoys.artifact.manager.JsonManager;
import java.sql.SQLException;
import com.sojoys.artifact.build.data.jdbc.SK_Query;
import com.google.common.collect.Lists;
import com.sojoys.artifact.tools.ToolMap;
import org.apache.commons.dbutils.handlers.MapHandler;
import com.sojoys.artifact.tools.SK_Plus;
import java.sql.ResultSet;
import java.util.Map;
import javax.sql.DataSource;
import org.game.db.bean.ServerMail;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sojoys.artifact.constant.CreateTableEnum;
import static org.game.db.StaticResource.ServerMailResource.*;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.xiaoleilu.hutool.log.Log;
import java.util.Set;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.xiaoleilu.hutool.util.StrUtil;
import com.sojoys.artifact.tools.ToolError;
import org.apache.commons.dbutils.DbUtils;
import com.xiaoleilu.hutool.log.LogFactory;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.dbutils.ResultSetHandler;
import com.google.common.base.Joiner;
import static org.game.db.StaticResource.ServerMailResource.*;
/**
 * 全服邮件
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ServerMailDaoImpl implements BaseDao<Integer,ServerMail>{
	static Log log = LogFactory.get(ServerMailDaoImpl.class);
	
	// 代理
	
	/**
	 * 私有化无参构造方法
	 */
	private ServerMailDaoImpl() {}
	  
    private static final ServerMailDaoImpl INSTANCE = new ServerMailDaoImpl();  
    
    public static ServerMailDaoImpl me() {  
        return INSTANCE;  
    }  
	
	public ServerMail insert(ServerMail serverMail){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(serverMail,conn);
	}
	
	public ServerMail insert(ServerMail serverMail,Connection conn){
		return insert(serverMail,conn,TABLE_NAME);
	}
	
	public ServerMail insert(ServerMail serverMail,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(serverMail,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public ServerMail insert(ServerMail serverMail,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(serverMail,conn,tableName);
	}
	
	public ServerMail insert(ServerMail serverMail,Connection conn,String tableName){
		
		SK_Query sq = new SK_Query();
		String sql = "INSERT INTO " +tableName+ " (id,unique_id,sender_id,text_type,title,body,items,pids_status,create_date,modified_date) VALUES (?,?,?,?,?,?,?,?,?,?)";
		try {
			int i = (int)sq.insert(conn,sql,serverMail.getId(),serverMail.getUniqueId(),serverMail.getSenderId(),serverMail.getTextType(),serverMail.getTitle(),serverMail.getBody(),serverMail.getItemsByte(),serverMail.getPidsStatusByte(),serverMail.getCreateDate(),serverMail.getModifiedDate());
			if(serverMail.getId()==0){
				serverMail.setId(i);
			}
			return i > 0 ? serverMail : null;
		} catch (Exception e) {
			log.error(e);
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public ServerMail insert(ServerMail serverMail,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(serverMail,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public int[] insertBatch(List<ServerMail> serverMails){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(serverMails,conn);
	}
	
	public int[] insertBatch(List<ServerMail> serverMails,Connection conn){
		return insertBatch(serverMails,conn,TABLE_NAME);
	}
	
	public int[] insertBatch(List<ServerMail> serverMails,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(serverMails,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] insertBatch(List<ServerMail> serverMails,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(serverMails,conn,tableName);
	}
	
	public int[] insertBatch(List<ServerMail> serverMails,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,unique_id,sender_id,text_type,title,body,items,pids_status,create_date,modified_date) VALUES (?,?,?,?,?,?,?,?,?,?)";
		try {
			int columnSize = 10;
			int size = serverMails.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =serverMails.get(i).getId();
				params[i][1] =serverMails.get(i).getUniqueId();
				params[i][2] =serverMails.get(i).getSenderId();
				params[i][3] =serverMails.get(i).getTextType();
				params[i][4] =serverMails.get(i).getTitle();
				params[i][5] =serverMails.get(i).getBody();
				params[i][6] =serverMails.get(i).getItemsByte();
				params[i][7] =serverMails.get(i).getPidsStatusByte();
				params[i][8] =serverMails.get(i).getCreateDate();
				params[i][9] =serverMails.get(i).getModifiedDate();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] insertBatch(List<ServerMail> serverMails,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(serverMails,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public ServerMail update(ServerMail serverMail){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(serverMail,conn);
	}
	
	public ServerMail update(ServerMail serverMail,Connection conn){
		return update(serverMail,conn,TABLE_NAME);
	}
	
	public ServerMail update(ServerMail serverMail,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(serverMail,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public ServerMail update(ServerMail serverMail,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(serverMail,conn,tableName);
	}
	
	public ServerMail update(ServerMail serverMail,Connection conn,String tableName){
		List<ServerMail> serverMails = new ArrayList<ServerMail>();
		serverMails.add(serverMail);
		updateBatch(serverMails,conn,tableName);
		return serverMail;
	}
	
	public ServerMail update(ServerMail serverMail,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(serverMail,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<ServerMail> serverMails){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(serverMails,conn);
	}
	
	public int[] updateBatch(List<ServerMail> serverMails,Connection conn){
		return updateBatch(serverMails,conn,TABLE_NAME);
	}
	
	public int[] updateBatch(List<ServerMail> serverMails,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(serverMails,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<ServerMail> serverMails,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(serverMails,conn,tableName);
	}
	
	public int[] updateBatch(List<ServerMail> serverMails,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET id = ?,unique_id = ?,sender_id = ?,text_type = ?,title = ?,body = ?,items = ?,pids_status = ?,create_date = ?,modified_date = ? WHERE id = ?";
		try {
			int columnSize = 10;
			int size = serverMails.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =serverMails.get(i).getId();
				params[i][1] =serverMails.get(i).getUniqueId();
				params[i][2] =serverMails.get(i).getSenderId();
				params[i][3] =serverMails.get(i).getTextType();
				params[i][4] =serverMails.get(i).getTitle();
				params[i][5] =serverMails.get(i).getBody();
				params[i][6] =serverMails.get(i).getItemsByte();
				params[i][7] =serverMails.get(i).getPidsStatusByte();
				params[i][8] =serverMails.get(i).getCreateDate();
				params[i][9] =serverMails.get(i).getModifiedDate();
				params[i][columnSize] =serverMails.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] updateBatch(List<ServerMail> serverMails,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(serverMails,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean delete(ServerMail serverMail){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(serverMail,conn);
	}
	
	public boolean delete(ServerMail serverMail,Connection conn){
		return delete(serverMail,conn,TABLE_NAME);
	}
	
	public boolean delete(ServerMail serverMail,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(serverMail,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean delete(ServerMail serverMail,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(serverMail,conn,tableName);
	}
	
	public boolean delete(ServerMail serverMail,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int i = run.update(conn,sql, serverMail.getId());
			return i > 0 ? true : false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean delete(ServerMail serverMail,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(serverMail,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public boolean deleteBatch(List<ServerMail> serverMails){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(serverMails,conn);
	}
	
	public boolean deleteBatch(List<ServerMail> serverMails,Connection conn){
		return deleteBatch(serverMails,conn,TABLE_NAME);
	}
	
	public boolean deleteBatch(List<ServerMail> serverMails,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(serverMails,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean deleteBatch(List<ServerMail> serverMails,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(serverMails,conn,tableName);
	}
	
	public boolean deleteBatch(List<ServerMail> serverMails,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int size = serverMails.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = serverMails.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean deleteBatch(List<ServerMail> serverMails,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(serverMails,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}

	/**
	 * 根据( id ) 查询
	 */
	public ServerMail getById(int id){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn);
	}
	
	public ServerMail getById(int id,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn,tableName);
	}
	public List<ServerMail> getIn(Set<Integer> ids){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn);
	}
	
	public List<ServerMail> getIn(Set<Integer> ids,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn,tableName);
	}
	/**
	 * 根据( unique_id ) 查询
	 */
	public ServerMail getByUniqueId(String uniqueId){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByUniqueId(uniqueId, conn);
	}
	
	public ServerMail getByUniqueId(String uniqueId,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByUniqueId(uniqueId, conn,tableName);
	}
	
	//Connection
	/**
	 * 根据( id ) 查询
	 */
	public ServerMail getById(int id,Connection conn){
		return getById(id,conn,TABLE_NAME);
	}
	
	public ServerMail getById(int id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,unique_id,sender_id,text_type,title,body,items,pids_status,create_date,modified_date FROM " + tableName + " WHERE " + "id = ? ORDER BY id ASC LIMIT 1";
		ServerMail serverMail = null; 
		try {
			serverMail = run.query(conn,sql, new ServerMailHandler(), id);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return serverMail;
	}
	public List<ServerMail> getIn(Set<Integer> ids,Connection conn){
		return getIn(ids,conn,TABLE_NAME);
	}
	
	public List<ServerMail> getIn(Set<Integer> ids,Connection conn,String tableName){
		List<ServerMail> serverMails = Lists.newArrayList();
		if(ids.isEmpty()) return serverMails;
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,unique_id,sender_id,text_type,title,body,items,pids_status,create_date,modified_date FROM " + tableName + " WHERE " + "id in (%s) ORDER BY id ASC";
		sql = String.format(sql, Joiner.on(",").join(ids));
		try {
			serverMails = run.query(conn,sql, new ServerMailListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return serverMails;
	}
	/**
	 * 根据( unique_id ) 查询
	 */
	public ServerMail getByUniqueId(String uniqueId,Connection conn){
		return getByUniqueId(uniqueId,conn,TABLE_NAME);
	}
	
	public ServerMail getByUniqueId(String uniqueId,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,unique_id,sender_id,text_type,title,body,items,pids_status,create_date,modified_date FROM " + tableName + " WHERE " + "unique_id = ? ORDER BY id ASC LIMIT 1";
		ServerMail serverMail = null; 
		try {
			serverMail = run.query(conn,sql, new ServerMailHandler(), uniqueId);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return serverMail;
	}
	
	//DataSource
	/**
	 * 根据( id ) 查询
	 */
	public ServerMail getById(int id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public ServerMail getById(int id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public List<ServerMail> getIn(Set<Integer> ids,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<ServerMail> getIn(Set<Integer> ids,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	/**
	 * 根据( unique_id ) 查询
	 */
	public ServerMail getByUniqueId(String uniqueId,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByUniqueId(uniqueId, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public ServerMail getByUniqueId(String uniqueId,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByUniqueId(uniqueId, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public List<ServerMail> getAll(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn);
	}
	
	public List<ServerMail> getAll(Connection conn){
		return getAll(conn,TABLE_NAME);
	}
	
	public List<ServerMail> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<ServerMail> getAll(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn,tableName);
	}
	
	public List<ServerMail> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,unique_id,sender_id,text_type,title,body,items,pids_status,create_date,modified_date FROM " + tableName + " ORDER BY id ASC";
		List<ServerMail> serverMails = Lists.newArrayList();
		try {
			serverMails = run.query(conn, sql, new ServerMailListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return serverMails;
	}
	
	public List<ServerMail> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public List<ServerMail> getAllPage(int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,page,pageSize);
	}
	
	public List<ServerMail> getAllPage(Connection conn,int page,int pageSize){
		return getAllPage(conn,TABLE_NAME,page,pageSize);
	}
	
	public List<ServerMail> getAllPage(DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<ServerMail> getAllPage(String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,tableName,page,pageSize);
	}
	
	public List<ServerMail> getAllPage(Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,unique_id,sender_id,text_type,title,body,items,pids_status,create_date,modified_date FROM " + tableName + " ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<ServerMail> serverMails = Lists.newArrayList();
		try {
			serverMails = run.query(conn, sql, new ServerMailListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return serverMails;
	}
	
	public List<ServerMail> getAllPage(DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean truncate(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn);
	}
	
	public boolean truncate(Connection conn){
		return truncate(conn,TABLE_NAME);
	}
	
	public boolean truncate(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean truncate(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn,tableName);
	}
	
	public boolean truncate(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "TRUNCATE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean truncate(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	// 执行自定义查询sql
	public List<Map<String,Object>> executeQuerySql(String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String sql){
		return executeQuerySql(conn,TABLE_NAME,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String sql){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,sql);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Map<String,Object>> executeQuerySql(String tableName,String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,tableName);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String tableName,String sql){
		QueryRunner run = new QueryRunner();
		ToolError.isAndTrue(ArtifactErrorCode.SQL_PERMISSIONS_ERROR, !StrUtil.startWithIgnoreCase(sql, "SELECT"));
		List<Map<String,Object>> list = Lists.newArrayList();
		try {
			list = run.query(conn,sql, new MapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return list;
	}
	/**
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	*/
	
	//
	
	public int getLastId(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getLastId(conn);
	}
	
	public int getLastId(Connection conn){
		return getLastId(conn,TABLE_NAME);
	}
	
	public int getLastId(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getLastId(conn);
		} catch (Exception e) {
			log.error(e);
			return 0;
		}
	}
	
	public int getLastId(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getLastId(conn,tableName);
	}
	
	public int getLastId(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id FROM " + tableName + " ORDER BY id DESC LIMIT 1";
		try {
			Map result = run.query(conn, sql, new MapHandler());
			return result == null ? 0 : ToolMap.getInt("id",result,0);
		} catch (Exception e) {
			log.error(e);
			return 0;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return 0;
			}
		}
	}
	
	public int getLastId(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getLastId(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return 0;
		}
	}
	//
	
	//Drop Table
	public boolean drop(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn);
	}
	
	public boolean drop(Connection conn){
		return drop(conn,TABLE_NAME);
	}
	
	public boolean drop(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return drop(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean drop(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn,tableName);
	}
	
	public boolean drop(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DROP TABLE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean drop(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return drop(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	//create
	public boolean createTable(CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,type);
	}
	
	public boolean createTable(Connection conn,CreateTableEnum type){
		return createTable(conn,TABLE_NAME,type);
	}
	
	public boolean createTable(DataSource ds,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean createTable(String tableName,CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,tableName,type);
	}
	
	public boolean createTable(Connection conn,String tableName,CreateTableEnum type){
		QueryRunner run = new QueryRunner();
		SK_Plus plus = SK_Plus.b("CREATE TABLE IF NOT EXISTS `", tableName,"` (");
		plus.a("  `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',");	
		plus.a("  `unique_id` VARCHAR(45)   COMMENT 'remarks=全服邮件唯一ID',");	
		plus.a("  `sender_id` INT(11)   COMMENT 'remarks=发送者',");	
		plus.a("  `text_type` INT(11)   COMMENT 'remarks=文字类型',");	
		plus.a("  `title` VARCHAR(45)   COMMENT 'remarks=标题',");	
		plus.a("  `body` VARCHAR(45)   COMMENT 'remarks=内容',");	
		plus.a("  `items` BLOB(65535)   COMMENT 'remarks=附件',");	
		plus.a("  `pids_status` BLOB(65535)   COMMENT 'remarks=玩家状态',");	
		plus.a("  `create_date` DATETIME NOT NULL  COMMENT '',");
		plus.a("  `modified_date` DATETIME   COMMENT '',");
		plus.a("  PRIMARY KEY (`id`),");
		if (CreateTableEnum.LOGIC.equals(type) || CreateTableEnum.DESIGN.equals(type)) {
			plus.a("  UNIQUE KEY `unique_id` (`unique_id`),");
			
		}
		if (CreateTableEnum.DESIGN.equals(type)) {
		}
		String sql = plus.e();
		sql = StrUtil.subPre(sql, sql.lastIndexOf(StrUtil.C_COMMA));
		sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 COMMENT='remarks=全服邮件';";
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean createTable(DataSource ds,String tableName,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,tableName,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public String getTableName(){
		return TABLE_NAME;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getColumnsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"id\",\"NULLABLE\":0,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"YES\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":1,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"unique_id\",\"NULLABLE\":1,\"REMARKS\":\"remarks=全服邮件唯一ID\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":2,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"sender_id\",\"NULLABLE\":1,\"REMARKS\":\"remarks=发送者\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":3,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"text_type\",\"NULLABLE\":1,\"REMARKS\":\"remarks=文字类型\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":4,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"title\",\"NULLABLE\":1,\"REMARKS\":\"remarks=标题\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":5,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"body\",\"NULLABLE\":1,\"REMARKS\":\"remarks=内容\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":6,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"items\",\"NULLABLE\":1,\"REMARKS\":\"remarks=附件\ntype=Map\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":7,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"pids_status\",\"NULLABLE\":1,\"REMARKS\":\"remarks=玩家状态\ntype=Map\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":8,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"create_date\",\"NULLABLE\":0,\"REMARKS\":\"\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":9,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"modified_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":10,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getIndexsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"id\",\"INDEX_NAME\":\"PRIMARY\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"server_mail\",\"COLUMN_NAME\":\"unique_id\",\"INDEX_NAME\":\"unique_id\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
//自定义内容起始位置
class ServerMailHandler implements ResultSetHandler<ServerMail>{
	@Override
	public ServerMail handle(ResultSet rs) throws SQLException {
		return rs.next() ? ServerMail.builder().createForResultSet(rs).syncIndexValues() : null ;
	}
}

class ServerMailListHandler extends AbstractListHandler<ServerMail>{
	@Override
	protected ServerMail handleRow(ResultSet rs) throws SQLException {
		return ServerMail.builder().createForResultSet(rs).syncIndexValues();
	}
}

//自定义内容结束位置
}

